package UI;

import Dao.JdbcTools;
import model.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
@WebServlet("/update")
public class UpdateShow extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1,获取更新的那一条记录的id
        Integer id = Integer.valueOf(req.getParameter("id"));
        Student student = null;

        try {
            student = JdbcTools.queryBean("select * from student where id=?", Student.class, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println("student = " + student);

        System.out.println("信息列表");
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        PrintWriter writer =resp.getWriter();
        writer.write("<!DOCTYPE html>");
        writer.write("<html>");
        writer.write("<head>");
        writer.write("</head>");
        writer.write("<title>学生信息更新页面</title>");
        writer.write("<body style='background:lightyellow'>");
        writer.write("<h1>学生信息更新页面</h1>");
        writer.write(  "<div style='margin:0 auto;width:600px;border:solid 3px red;'>"+
                "<h3><a href='update'>请选择你要更新的字段</a></h3>");
        writer.write("<form action = 'updateok'>"+
                "<input type ='hidden' name='id' value="+
                student.getId()+
                " name='name'><br>"+
                "姓名：<input type='text' value="+
                student.getName()+
                " name='name'><br>"+
                "性别：<input type='text' value="+
                student.getSex()+
                " name='sex'><br>"+
                "年龄：<input type='text' value="+
                student.getAge()+
                " name='age'><br>"+
                "专业：<input type='text' value="+
                student.getMajor()+
                " name='major'><br>"+
                " <button>更新</button> "+
                "<input type ='reset' value='重置'/>"+
                "</form>"+
                "</div>");
        writer.write("</body>");
        writer.write("</html>");



    }
}
